package com.yanyilu.basing.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yanyilu.basing.common.R;
import com.yanyilu.basing.entity.Activity;
import com.yanyilu.basing.entity.CheckInRecord;
import com.yanyilu.basing.entity.UnboxRecord;
import com.yanyilu.basing.mapper.ActivityMapper;
import com.yanyilu.basing.mapper.CheckInRecordMapper;
import com.yanyilu.basing.mapper.UnboxRecordMapper;
import com.yanyilu.basing.service.ICheckInRecordService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yanyilu.basing.service.ITotalTimeMileageService;
import com.yanyilu.basing.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Sxx
 * @since 2020-10-26
 */
@Service
public class CheckInRecordServiceImpl extends ServiceImpl<CheckInRecordMapper, CheckInRecord> implements ICheckInRecordService {
    @Autowired
    CheckInRecordMapper checkInRecordMapper;

    @Autowired
    UnboxRecordMapper unboxRecordMapper;

    @Autowired
    ITotalTimeMileageService iTotalTimeMileageService;

    @Autowired
    ActivityMapper activityMapper;




    @Override
    public R getRecordList(Integer userId, Integer limit, Integer page) {
        PageHelper.startPage(page,limit);
        List<CheckInRecord> list = checkInRecordMapper.selectAllByUserId(userId);
        PageInfo<CheckInRecord> pageInfo = new PageInfo<>(list);
        return R.ok().put("list",pageInfo.getList()).put("count",pageInfo.getTotal()).put("pages",pageInfo.getPages());
    }

    /**
     * 添加打卡记录
     * @param checkInRecord
     * @return
     */
    @Override
    public R addCheckInRecord(CheckInRecord checkInRecord) {
        //获取当前时间戳
        int currentTime = TimeUtil.getTime();
        //获取活动时间
        Activity activity = activityMapper.selectById(1);
        Integer startTime = activity.getStartTime();
        Integer endTime = activity.getEndTime();
        if( currentTime < startTime){
            return R.error("活动尚未开始！");
        }else if(endTime < currentTime){
            return R.error("当前活动已结束！");
        }
        UnboxRecord unboxRecord = unboxRecordMapper.selectByUserIdAndStatus(checkInRecord.getUserId(),1);
        if(null == unboxRecord){
            return R.error("还未开箱或还未通过审核！");
        }
        //设置时间
        checkInRecord.setCheckInDate(TimeUtil.getTime());
        //设置审核状态
        checkInRecord.setReviewState(0);
        Integer insert = checkInRecordMapper.insert(checkInRecord);
        return insert == 1 ? R.ok():R.error();
    }

    @Override
    public R getAdminRecordList(Integer reviewState, Integer limit, Integer page) {
        PageHelper.startPage(page,limit);
        if(!(null==reviewState)){
            List<CheckInRecord> list = checkInRecordMapper.selectAdminAllBystate(reviewState);
            PageInfo<CheckInRecord> pageInfo = new PageInfo<>(list);
            return R.ok().put("list",pageInfo.getList()).put("count",pageInfo.getTotal()).put("pages",pageInfo.getPages());
        }else {
            List<CheckInRecord> list = checkInRecordMapper.selectAdminAll();
            PageInfo<CheckInRecord> pageInfo = new PageInfo<>(list);
            return R.ok().put("list",pageInfo.getList()).put("count",pageInfo.getTotal()).put("pages",pageInfo.getPages());
        }
    }

    /**
     * 打卡记录审核
     * @param checkInRecord
     * @return
     */
    @Transactional
    @Override
    public R adminUpdate(CheckInRecord checkInRecord){

        CheckInRecord checkInRecord1 = checkInRecordMapper.selectById(checkInRecord.getCheckInRecordId());
        checkInRecord1.setReviewState(checkInRecord.getReviewState());
        if(checkInRecord1.getIsState() == 0){
            if(checkInRecord.getReviewState() == 1){
                checkInRecord1.setIsState(1);
                iTotalTimeMileageService.addNumber(checkInRecord1.getTimeSpent(),checkInRecord1.getMileage(),checkInRecord1.getUserId());
            }
        }else {
            if(checkInRecord.getReviewState() == 2){
                checkInRecord1.setIsState(0);
                iTotalTimeMileageService.addNumber((-1)*checkInRecord1.getTimeSpent(),(-1)*checkInRecord1.getMileage(),checkInRecord1.getUserId());
            }
        }
        checkInRecordMapper.updateById(checkInRecord1);
        return R.ok();
    }
}
